package com.siysoft.tc.admin.service.Impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.siysoft.tc.admin.service.IAdminUserReservationService;
import com.siysoft.tc.base.bean.to.Reservation;
import com.siysoft.tc.base.bean.to.UserReservation;
import com.siysoft.util.IMap;
import com.tc.base.dao.BaseDAO;

/**
 * 方法摘要：实现后台管理员管理功能接口 作者：zhouyu 日期：2015年6月5日
 * 
 * @param iMap
 * @return
 * @throws Exception
 */

@Service
public class AdminUserReservationServiceImpl implements
		IAdminUserReservationService {
	@Autowired
	BaseDAO baseDao;

	@Override
	public List<Map<String, Object>> seekUserReservations(IMap iMap) throws Exception {
		// TODO Auto-generated method stub
		String hql = "select * from v_user_reservation order by user_reservation_id desc";
		return (List<Map<String, Object>>) baseDao.findBySQLToMap(hql, null, -1, -1);
	}

	@Override
	public Map<String, String> toUserReservationRevise(IMap iMap) throws Exception {
		// TODO Auto-generated method stub
		String hql = "select * from v_user_reservation "
				+ "where user_reservation_id = "
				+ iMap.getInt("user_reservation_id")
				+ " ";
		// 将查询结果存到list下
		List list = baseDao.findBySQLToMap(hql, iMap, -1, -1);
		// 判断list中是否存在数据
		if (list.size() > 0) {
			// list中存在数据返回list第一条数据
			return (Map<String, String>) list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void reviseUserReservationInfo(IMap iMap) {
		// TODO Auto-generated method stub
		UserReservation user_reservation = (UserReservation)baseDao.findById(UserReservation.class, iMap.getInt("user_reservation_id"));
		Integer user_reservation_status = Integer.parseInt(iMap.get("user_reservation_status").toString());
		user_reservation.setUser_reservation_status(user_reservation_status);
		Integer reservation_id = user_reservation.getReservation_id();
		baseDao.update(user_reservation);
		String hql = "from UserReservation where reservation_id = " + reservation_id ;
		List<UserReservation> list = baseDao.findByHQL(hql);
		int flag = 1;
		for(int i = 0;i<list.size();i++)
		{
			UserReservation ur = list.get(i);
			if(ur.getUser_reservation_status() == 1  )flag = 0;
		}
		if(flag == 1)
		{
			Reservation rs = (Reservation)baseDao.findById(Reservation.class, reservation_id);
			rs.setReservation_status(2);
			baseDao.save(rs);
		}
	}

	@Override
	public void removeUser(IMap iMap) {
		Integer user_id = iMap.getInt("user_id");
		Integer reservation_id = iMap.getInt("reservation_id");
		String hql1 = "from UserReservation where user_id="+user_id+" and reservation_id="+reservation_id;
		List<UserReservation> ur = baseDao.findByHQL(hql1);
		if(ur.size() > 0){
			if(ur.get(0).getUser_reservation_status() >= 2){
				return ;
			}
			baseDao.delete(ur.get(0));
			Reservation r = (Reservation) baseDao.findById(Reservation.class, reservation_id);
			r.setReservation_current_size(r.getReservation_current_size()-1);
			if(r.getReservation_status() != 2){
				String hql2 = "from UserReservation where reservation_id="+reservation_id;
				List<UserReservation> ur2 = baseDao.findByHQL(hql2);
				Integer flag = 0;
				for(Integer i = 0; i < ur2.size(); i++){
					if(ur2.get(i).getUser_reservation_status() < 2){
						flag = 1;
						break;
					}
				}
				if(flag == 0){
					r.setReservation_status(2);
				}
			}
			baseDao.update(r);
		}
		// TODO Auto-generated method stub
		
	}
	
		
}
